package my.jwds.p838;

public class PushDominoes {

    public static void main(String[] args) {
        PushDominoes obj = new PushDominoes();
        System.out.println(obj.pushDominoes("R.R.L"));
    }

    public String pushDominoes(String dominoes) {
        char now = dominoes.charAt(0);
        int nowIndex = 0;
        StringBuilder res = new StringBuilder();
        for (int i = 1; i < dominoes.length(); i++) {
            char c = dominoes.charAt(i);
            if (c == 'L'){
                if (now == 'L' || now == '.'){
                    for (int j = nowIndex; j < i; j++) {
                        res.append('L');
                    }
                }else{
                    int diff = i - nowIndex - 1;
                    for (int z = 0; z < diff/2+1; z++) {
                        res.append(now);
                    }
                    if (diff % 2 == 1){
                        res.append('.');
                    }
                    for (int z = 0; z < diff/2; z++) {
                        res.append(c);
                    }
                }
            }else if (c == 'R'){
                if (now == 'R'){
                    for (int j = nowIndex; j < i; j++) {
                        res.append('R');
                    }
                }else{
                    for (int j = nowIndex; j < i; j++) {
                        res.append(dominoes.charAt(j));
                    }
                }
            }
            if (c == 'L' || c == 'R'){
                now = c;
                nowIndex = i;
            }
        }

        if (now == 'R'){
            for (int i = nowIndex; i < dominoes.length(); i++) {
                res.append(now);
            }
        }else if (now == 'L' || now == '.'){
            for (int i = nowIndex; i < dominoes.length(); i++) {
                res.append(dominoes.charAt(i));
            }
        }
        return res.toString();
    }
}